package com.member.mp.job;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.member.common.utils.DateUtils;
import com.member.common.utils.StringUtils;
import com.member.shop.domain.SysBizOrder;
import com.member.shop.domain.SysBizRechargeUser;
import com.member.shop.service.*;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBlockingQueue;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @date 2024/8/30 15:09
 */
@Slf4j
@Component
public class OrderTask {

    @Resource
    private RBlockingQueue<String> blockingQueue;
    @Resource
    private ISysBizOrderService sysBizOrderService;
    @Resource
    private ISysBizRechargeUserService sysBizRechargeUserService;

    @PostConstruct
    public void take() {
        log.info("订单超时处理任务启动");
        ExecutorService executorService = Executors.newFixedThreadPool(1);
        executorService.execute(new Thread(() -> {
            while (true) {
                try {
                    //将到期的数据取出来，如果一直没有到期数据，就一直等待。
                    String key = blockingQueue.take();
                    if (key.startsWith("ORDER_")) {
                        String payId = key.replace("ORDER_", StringUtils.EMPTY);
                        SysBizOrder sysBizOrder = sysBizOrderService.getOne(new LambdaQueryWrapper<SysBizOrder>().eq(SysBizOrder::getPaymentId, payId));
                        log.info("时间到了取消-------------------------{}", payId);
                        log.info("时间到了取消SysBizOrder---------------------{}", sysBizOrder);
                        if (sysBizOrder != null && sysBizOrder.getStatus() == 10) {
                            Date nowDate = DateUtils.getNowDate();
                            sysBizOrder.setCancelTime(nowDate);
                            sysBizOrder.setCancelReason("支付超时");
                            sysBizOrder.setUpdateTime(nowDate);
                            sysBizOrder.setStatus(90);
                            sysBizOrderService.updateById(sysBizOrder);
                            log.info("{}订单支付超时", key);
                        }
                    }

                    if (key.startsWith("RECHARGE_SURPLUS_")) {
                        log.info("{}充值订单支付超时", key);
                        String payId = key.replace("RECHARGE_SURPLUS_", StringUtils.EMPTY);
                        SysBizRechargeUser sysBizRechargeUser = sysBizRechargeUserService.getOne(new LambdaQueryWrapper<SysBizRechargeUser>().eq(SysBizRechargeUser::getPaymentNo, payId).eq(SysBizRechargeUser::getPaymentStatus, 0));
                        if (sysBizRechargeUser != null) {
                            sysBizRechargeUser.setPaymentStatus(2);
                            sysBizRechargeUser.setRemark("支付超时");
                            sysBizRechargeUserService.updateById(sysBizRechargeUser);
                            log.info("{}充值订单支付超时结束", key);
                        }
                    }

                    log.info(key);
                } catch (InterruptedException e) {
                    log.error(e.getMessage());
                }
            }
        }));
    }
}

